package com.qitmiaojie.controller;

import com.qitmiaojie.controller.utils.Result;
import com.qitmiaojie.pojo.Malfunction;

import com.qitmiaojie.service.MalfunctionService;
import com.qitmiaojie.service.SendEmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hpsf.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
public class MalfunctionController {

    @Autowired
    private MalfunctionService malfunctionService;
    @Autowired
    private SendEmailService sendEmailService;

    /**
     * 宿舍报修
     * @param malfunction
     * @return
     * @throws Exception
     */
    @PreAuthorize("hasAuthority('test:manage:sushe')")
    @PostMapping("/malfunction")
    public Result malfunction(@RequestBody Malfunction malfunction) throws Exception {
        System.out.println(malfunction);
        log.info(malfunction.toString());
        return malfunctionService.insetMalfunction(malfunction);
/*        return malfunctionService.insetMalfunction(malfunction);*/
    }

    /**
     * 根据id查询该用户所有保修
     * id 从security域中获得
     * @return
     */
    @GetMapping("/malfunction")
    public Result getAllById(){
        return malfunctionService.getAllById();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/malfunction/{id}")
    public Result del(@PathVariable Integer id){
        return malfunctionService.del(id);
    }

    /**
     * 评分
     * @param id
     * @param value
     * @param advice
     * @return
     */
    @PreAuthorize("hasAuthority('baoxiu:manage:value')")
    @PutMapping("/malfunction/evaluate/{id}/{value}/{advice}")
    public Result evaluate(@PathVariable Integer id,@PathVariable Integer value,@PathVariable String advice){
        log.info("{},{},{}",id,value,advice);
       return malfunctionService.evaluate(id,value,advice);
    }

    /**
     * 宿管查询所有报修
     * 分页条件查找
     * @return
     */
    @GetMapping("/malfunctionAll")
    /*
    * date:this.searchForm.date,
                        floor:this.searchForm.floor,
                        address:this.searchForm.address,
                        phone:this.searchForm.Id,
                        status: this.status,
                        currentPage:this.page.CurrentPage,
                        size:this.page.Size,*/
    public Result getMalfunctionAll(@RequestParam(name = "date",required = false) String date,
                                    @RequestParam(name = "floor",required = false) String floor,
                                    @RequestParam(name = "address",required = false) String address,
                                    @RequestParam(name = "phone",required = false) String phone,
                                    @RequestParam(name = "status",required = false) Integer status,
                                    @RequestParam(name = "currentPage",required = false)Integer currentPage,
                                    @RequestParam(name = "size",required = false)Integer size){

        log.info("{},{},{},{},{},{},{}",date,floor,address,phone,status,currentPage,size);
        return malfunctionService.getMalfunctionAll(date,floor,address,phone,status,currentPage,size);
    }
    @PutMapping("/malfunctionAll/{id}/{status}")
    public Result updateMalfunctionStatusById(@PathVariable("id") Integer id ,@PathVariable("status") Integer status){
        log.info("{},{}",id,status);
        return malfunctionService.updateMalfunctionStatusById(id,status);

    }
}
